

# 实验8
import PRi.GPIO as GPIO
import time

makerobo_VibratePin = 11
makerobo_Rpin = 12
makerobo_Gpin = 13

clb_tmp = 0


def makerobo_setup():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    GPIO.setup(makerobo_Rpin, GPIO.OUT)
    GPIO.setup(makerobo_Gpin, GPIO.OUT)
    GPIO.setup(makerobo_VibratePin, GPIO.IN, pull_up_down=GPIO.PUD_UP)


def double_colorLED(x):
    if x == 0:
        GPIO.output(makerobo_Rpin, 1)
        GPIO.output(makerobo_Gpin, 0)
    if x == 1:
        GPIO.output(makerobo_Rpin, 0)
        GPIO.output(makerobo_Gpin, 1)


def makerobo_Print(x):
    global clb_tmp
    if x != clb_tmp:
        if x == 0:
            print(' ****************** ')
            print(' * Makerobo    ON * ')
            print( '****************** ')
        
        if x == 1:
            print(' ****************** ')
            print(' * OFF   Makerobo * ')
            print( '****************** ')
        clb_tmp = x


def makerobo_loop():
    clb_state = 0
    while True:
        if GPIO.input(makerobo_VibratePin)==1:
            clb_state = clb_state +1
            if clb_state > 1:
                clb_state =0
            double_colorLED(clb_state)
            makerobo_Print(clb_state)
            time.sleep(1)


def makerobo_destroy():
    GPIO.output(makerobo_Rpin, GPIO.HEGH)
    GPIO.output(makerobo_Gpin, GPIO.HEGH)
    GPIO.cleanup()


if __name__ == '__main__':
    makerobo_setup()
    try:
        makerobo_loop()
    except KeyboardInterrupt:
        makerobo_destroy()



# 实验9有源蜂鸣器
import PRi.GPIO as GPIO
import time

makerobo_Buzzer = 11

def makerobo_setup(pin):
    global makerobo_BuzzerPin
    makerobo_BuzzerPin = pin
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    GPIO.setup(makerobo_BuzzerPin, GPIO.OUT)
    GPIO.output(makerobo_BuzzerPin, GPIO.HIGH)


def makerobo_buzzer_on():
    GPIO.output(makerobo_BuzzerPin, GPIO.LOW)


def makerobo_buzzer_off():
    GPIO.output(makerobo_BuzzerPin, GPIO.HIGH)


def makerobo_beep(x):
    makerobo_buzzer_on()
    time.sleep(x)
    makerobo_buzzer_off()
    time.sleep(x)


def loop():
    while True:
        makerobo_beep(0.5)

def destroy():
    GPIO.output(makerobo_BuzzerPin, GPIO.HIGH)
    GPIO.cleanup()


if __name__ == '__main__':
    makerobo_setup(makerobo_Buzzer)
    try:
        loop()
    except KeyboardInterrupt:
        destroy()


# 实验9无源蜂鸣器
import PRi.GPIO as GPIO
import time

makerobo_Buzzer = 11

Tone_CL = [0, 131, 147, 165, 175, 196, 211, 248]
Tone_CM = [0, 262, 294, 330, 350, 393, 441, 495]
Tone_CH = [0, 525, 589, 661, 700, 786, 882, 990]

makerobo_song_1 = [Tone_CM[3], Tone_CM[5], Tone_CM[6], Tone_CM[3], Tone_CM[2], Tone_CM[3], Tone_CM[5], Tone_CM[6],
                   Tone_CH[1], Tone_CM[6], Tone_CM[5], Tone_CM[1], Tone_CM[3], Tone_CM[2], Tone_CM[2], Tone_CM[3], 
                   Tone_CM[5], Tone_CM[2], Tone_CM[3], Tone_CM[3], Tone_CL[6], Tone_CL[6], Tone_CL[6], Tone_CM[1], 
                   Tone_CM[2], Tone_CM[3], Tone_CM[2], Tone_CL[7], Tone_CL[6], Tone_CM[1], Tone_CL[5]  ]

makerobo_beat_1 = [ 1, 1, 3, 1, 1, 3, 1, 1, 
                    1, 1, 1, 1, 1, 1, 3, 1,
                    1, 3, 1, 1, 1, 1, 1, 1,
                    1, 2, 1, 1, 1, 1, 1, 1,
                    1, 1, 3 ]

makerobo_song_2 = [Tone_CM[1], Tone_CM[1], Tone_CM[1], Tone_CL[5], Tone_CM[3], Tone_CM[3], Tone_CM[3], Tone_CM[1],
                   Tone_CM[1], Tone_CM[3], Tone_CM[5], Tone_CM[5], Tone_CM[4], Tone_CM[3], Tone_CM[2], Tone_CM[2], 
                   Tone_CM[3], Tone_CM[4], Tone_CM[4], Tone_CM[3], Tone_CM[2], Tone_CM[3], Tone_CM[1], Tone_CM[1], 
                   Tone_CM[3], Tone_CM[2], Tone_CL[5], Tone_CL[7], Tone_CM[2], Tone_CM[1]  ]

makerobo_beat_2 = [ 1, 1, 2, 2, 1, 1, 2, 2, 
                    1, 1, 2, 2, 1, 1, 3, 1,
                    1, 2, 2, 1, 1, 2, 2, 1,
                    1, 2, 2, 1, 1, 3 ]

def makerobo_setup():
    makerobo_BuzzerPin = pin
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    GPIO.setup(makerobo_Buzzer, GPIO.OUT)
    global makerobo_Buzz
    makerobo_Buzz = GPIO.PWM(makerobo_Buzzer, 440)
    makerobo_Buzz.start(50)


def makerobo_loop():
    while True:
        for i in range(1, len(makerobo_song_1)):
            makerobo_Buzz.ChangeFrequency(makerobo_song_1[i])
            time.sleep(makerobo_beat_1[i] * 0.5)
        time.sleep(1)

        for i in range(1, len(makerobo_song_2)):
            makerobo_Buzz.ChangeFrequency(makerobo_song_1[i])
            time.sleep(makerobo_beat_1[i] * 0.5)  


def makerobo_destroy():
    makerobo_Buzz.stop()
    GPIO.output(makerobo_Buzzer, 1)
    GPIO.cleanup()


if __name__ == '__main__':
    makerobo_setup()
    try:
        makerobo_loop()
    except KeyboardInterrupt:
        makerobo_destroy()


# 实验10干簧管传感器实验
import PRi.GPIO as GPIO

makerobo_ReedPin = 11
makerobo_Rpin = 12
makerobo_Gpin = 13


def makerobo_setup():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    GPIO.setup(makerobo_Rpin, GPIO.OUT)
    GPIO.setup(makerobo_Gpin, GPIO.OUT)
    GPIO.setup(makerobo_ReedPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.add_event_detect(makerobo_ReedPin, GPIO.BOTH, callback=makerobo_detect, bouncetime=10)

def double_colorLED(x):
    if x == 0:
        GPIO.output(makerobo_Rpin, 1)
        GPIO.output(makerobo_Gpin, 0)
    if x == 1:
        GPIO.output(makerobo_Rpin, 0)
        GPIO.output(makerobo_Gpin, 1)


def makerobo_Print(x):
    if x == 0:
        print(' ******************************************** ')
        print(' ***** Makerobo Detect Magnetic Material!   * ')
        print(' ******************************************** ')


def makerobo_detect(chn):
    double_colorLED(GPIO.imput(makerobo_ReedPin))
    makerobo_Print(GPIO.input(makerobo_ReedPin))


def loop():
    while True:
        pass
        


def destroy():
    GPIO.output(makerobo_Rpin, GPIO.HEGH)
    GPIO.output(makerobo_Gpin, GPIO.HEGH)
    GPIO.cleanup()


if __name__ == '__main__':
    makerobo_setup()
    try:
        loop()
    except KeyboardInterrupt:
        destroy()


# 实验11U型光电传感器
import PRi.GPIO as GPIO

makerobo_PIPin = 11
makerobo_Rpin = 12
makerobo_Gpin = 13


def makerobo_setup():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    GPIO.setup(makerobo_Rpin, GPIO.OUT)
    GPIO.setup(makerobo_Gpin, GPIO.OUT)
    GPIO.setup(makerobo_PIPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.add_event_detect(makerobo_PIPin, GPIO.BOTH, callback=makerobo_detect, bouncetime=200)

def double_colorLED(x):
    if x == 1:
        GPIO.output(makerobo_Rpin, 1)
        GPIO.output(makerobo_Gpin, 0)
    if x == 0:
        GPIO.output(makerobo_Rpin, 0)
        GPIO.output(makerobo_Gpin, 1)


def makerobo_Print(x):
    if x == 1:
        print(' ************************************* ')
        print(' ***** Makerobo Light was blocked!   * ')
        print(' ************************************* ')


def makerobo_detect(chn):
    double_colorLED(GPIO.input(makerobo_PIPin))
    makerobo_Print(GPIO.input(makerobo_PIPin))


def loop():
    while True:
        pass
        


def destroy():
    GPIO.output(makerobo_Rpin, GPIO.HEGH)
    GPIO.output(makerobo_Gpin, GPIO.HEGH)
    GPIO.cleanup()


if __name__ == '__main__':
    makerobo_setup()
    try:
        loop()
    except KeyboardInterrupt:
        destroy()
